package com.xlh.controller;
import com.alibaba.fastjson.JSON;
import com.xlh.domain.Order;
import com.xlh.domain.Product;
import com.xlh.service.OrderService;
import com.xlh.service.ProductService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;
//@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProductService productService;
   /* @Autowired
   private RocketMQTemplate rocketMQTemplate;*/

    //下单测试4  通过feign调用
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        Product product = productService.findByPid(pid);

        if (product.getPid() == -100) {
            Order order = new Order();
            order.setOid(-100L);
            order.setPname("下单失败");
            return order;
        }

        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername(" 测 试 用 户 ");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.createOrder(order);
        //下单成功之后,将消息放到mq中
   /*     rocketMQTemplate.convertAndSend("order-topic", order);*/
        return  order;
    }
   /* //下单测试3  通过nacos 使用服务名调用   自定义负载均衡
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        //通过restTemplate调用商品微服务
        //调用商品微服务,查询商品信息
        *//*List<ServiceInstance> instances =  discoveryClient.getInstances("service-product");
        int random = new Random().nextInt( instances.size());
        ServiceInstance instance =  instances.get(0);
        Product product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/" + pid, Product.class);*//*
        //ribbon负载均衡
        Product product = restTemplate.getForObject("http://service-product/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername(" 测 试 用 户 ");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.createOrder(order);
        return  order;
      }*/
    /*//下单测试2 通过nacos 使用服务名调用
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        //通过restTemplate调用商品微服务
        //调用商品微服务,查询商品信息
        List<ServiceInstance> instances =  discoveryClient.getInstances("service-product");
        ServiceInstance instance =  instances.get(0);
        Product product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername(" 测 试 用 户 ");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.createOrder(order);
        return  order;
      }*/
    //下单测试1 原始方式调用 http://localhost:8081/product
/*    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        //1 一旦服务提供者的地址信息变化了,我们就不得不去修改服务调用者的java代码
        //2 一旦无法提供者做了集群,服务调用者一方无法实现负载均衡的去调用
        //3 一旦微服务变得越来越多,如何来管理这个服务清单就成了问题
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        //通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername(" 测 试 用 户 ");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.createOrder(order);
        return  order;
      }*/
    }
